package com.qc.searchBug.util;

import java.io.File;
import java.io.UnsupportedEncodingException;

import javax.mail.internet.MimeUtility;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

public class SendEmailUtil {
	public static void main(String[] args) {
		SendEmailUtil.sendSimpleEmail("dsadsad", "dsadas", "243237408@qq.com");
	}
	/**
	 * 发送简单邮件
	 */
	public static void sendSimpleEmail(String title,String content,String emailAddress){
		SimpleEmail email=new SimpleEmail();
		//设置smtp服务器
		email.setHostName("smtp.163.com");
		email.setDebug(true);
		//设置授权信息
		email.setAuthentication("q18300601114@163.com", "wangchao962464");
		email.setCharset("utf-8");
		try {
			//设置发件人信息【发件人邮箱，发件人姓名，编码格式】
			email.setFrom("q18300601114@163.com", "q18300601114@163.com", "utf-8");
			// 设置收件人信息[收件人邮箱，收件人姓名，编码格式]
			email.addTo(emailAddress, emailAddress, "utf-8");
			//设置主题
			email.setSubject(title);
			//设置邮件内容
			email.setMsg(content);
			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 发送HTML邮件
	 */
	public static  void sendEmailHtml() {
		HtmlEmail email = new HtmlEmail();

		email.setDebug(true);
		email.setHostName("smtp.sina.com");

		email.setAuthentication("hx_xiaodao@sina.com", "mail@sina");
		email.setCharset("UTF-8");

		try {
			email.setFrom("hx_xiaodao@sina.com", "小强", "UTF-8");

			email.addTo("243237408@qq.com", "243237408@qq.com", "utf-8");
			// http://www.baidu.com
			email.setSubject("HTML邮件3");
			email.setHtmlMsg("<div style='color:red;font-size:32px'>邮件正文</div><img src='https://timgsa.baidu.com/timg?image&amp;quality=80&amp;size=b9999_10000&amp;sec=1489746992176&amp;di=1bf7c590e954be38968f2b3b5f7effc7&amp;imgtype=0&amp;src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fdbb44aed2e738bd4a59870f4a58b87d6267ff9be.jpg' />");
			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
	//发送带图片的HTML文件
	public static void sendEmailHtml2() {
		HtmlEmail email = new HtmlEmail();

		email.setDebug(true);
		email.setHostName("smtp.sina.com");

		email.setAuthentication("hx_xiaodao@sina.com", "mail@sina");
		email.setCharset("UTF-8");

		try {
			email.setFrom("hx_xiaodao@sina.com", "小强", "UTF-8");

			email.addTo("2760305461@qq.com", "2760305461@qq.com", "utf-8");
			// http://www.baidu.com
			email.setSubject("HTML邮件5");
			String cid = email.embed(new File("C:/timg.jpg"));
			email.setHtmlMsg("<div style='color:red;font-size:30px'>HTML内容</div> <img src='cid:" + cid + "' />");

			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 发送复杂邮件
	 */
	public static void multiPartEmail() {

		MultiPartEmail email = new MultiPartEmail();
		email.setDebug(true);
		
		email.setHostName("smtp.sina.com");
		email.setAuthentication("hx_xiaodao@sina.com", "mail@sina");
		email.setCharset("utf-8");

		try {
			email.setFrom("hx_xiaodao@sina.com", "小强", "UTF-8");
			email.addTo("652331247@qq.com", "652331247@qq.com", "utf-8");

			email.setSubject("小公主。这是有你的快递");
			email.setMsg("一大波礼物");
		} catch (EmailException e1) {
			e1.printStackTrace();
		}

		// 为邮件添加附加内容
		EmailAttachment attachment = new EmailAttachment();
		attachment.setPath("D:\\SpringMVC笔记.txt");// 本地文件

		// attachment.setURL(new URL("http://xxx/a.gif"));//远程文件
		// attachment.setDisposition(EmailAttachment.ATTACHMENT);
		// attachment.setDescription("描述信息");

		try {
			// 设置附件显示名字[下载保存的名字和后缀]，必须要编码，不然中文会乱码
			attachment.setName(MimeUtility.encodeText("SpringMVC笔记.txt"));
			// 将附件添加到邮件中
			email.attach(attachment);
			email.send();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
}
